package com.db.user.config;

import com.db.user.filter.TokenFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

/**
 * 作者：Bin
 * 时间：2023/10/16
 * 描述：键盘敲烂，薪资过万
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/user/login","/user/verify")
                .permitAll()
                .anyRequest()
                .authenticated();
        //禁用csrfFilter过滤器
        http.csrf().disable();
        //允许跨域访问
        http.cors();
        //向过滤器链中加入自定义过滤器
        http.addFilterBefore(new TokenFilter(authenticationManager()), FilterSecurityInterceptor.class);
    }
}
